//
//  SISSPriceMapSearchViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/12/1.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSPriceMapSearchViewController.h"
#import "SISSPriceMapSearchViewCell.h"
#import "SISSJsonRequestNetworkingManager+Map.h"
#import "SISSMapBuildInfoModel.h"
#import "NSBundle+PYSearchExtension.h"

@interface SISSPriceMapSearchViewController ()<UISearchBarDelegate>

///
@property (weak, nonatomic) UISearchBar *searchBar;
@property (weak, nonatomic) UIView *lineView;

///
@property (strong, nonatomic) NSArray<SISSMapSearchPnameModel *> *pnameArr;

@end

@implementation SISSPriceMapSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = YES;
    [super viewWillAppear:animated];
    
    [self.searchBar becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBarHidden = NO;
    [super viewWillDisappear:animated];
}

#pragma mark - UISearchBarDelegate

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [self.searchBar resignFirstResponder];

    // 如果用户没有实现代理方法 默认为：dismiss ViewController
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self.searchBar resignFirstResponder];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    // 处理搜索结果(可用于动态修改搜索结果)
//    NSLog(@"%@ -- %@",searchBar.text,searchText);
    if (searchText.length == 0) return;
    [kJsonRequestNetworkingManager requestFuzzyQueryPnameListWithKeyWords:searchText successBlock:^(NSArray<SISSMapSearchPnameModel *> *pnameArr) {
        
        self.pnameArr = pnameArr;
        [self.tableView reloadData];
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.pnameArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSPriceMapSearchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:priceMapSearchViewCellReuseID forIndexPath:indexPath];
    cell.searchPnameModel = self.pnameArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.selectCellBlock) {
        SISSMapSearchPnameModel *searchPnameModel = self.pnameArr[indexPath.row];
        SISSMapBuildInfoModel *mapBuildInfo = [[SISSMapBuildInfoModel alloc] init];
        mapBuildInfo.btype = searchPnameModel.sellMaxBtype;
        mapBuildInfo.mapPoint = searchPnameModel.mapPoint;
        mapBuildInfo.sellPrice = searchPnameModel.sellPrice;
        mapBuildInfo.sellRingRatio = searchPnameModel.btypeSellRingPrice;
        mapBuildInfo.rentPrice = searchPnameModel.rentPrice;
        //        mapBuildInfo.rentRingRatio = searchPnameModel.rentPrice;
        mapBuildInfo.pname = searchPnameModel.buildName;
        self.selectCellBlock(mapBuildInfo);
        [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark - 导航栏
- (void)setupNavigationItems {
    [super setupNavigationItems];
    
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.delegate = self;
    searchBar.showsCancelButton = YES;
    searchBar.placeholder = @"请输入楼盘名";
    searchBar.barTintColor = MAIN_TEXT_COLOR;
    searchBar.tintColor = SECONDARY_TEXT_COLOR;
    searchBar.backgroundImage = [NSBundle py_imageNamed:@"clearImage"];

//    for (UIView *subview in searchBar.subviews) {
//        for(UIView* grandSonView in subview.subviews){
//            if ([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
//                grandSonView.alpha = 0.0f;
//            }else if([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){
//                NSLog(@"Keep textfiedld bkg color");
//            }else{
//                grandSonView.alpha = 0.0f;
//            }
//        } // for cacheViews
//    } // subviews
    UITextField *searchField;
    if (@available(iOS 13.0, *)) {
        searchField = searchBar.searchTextField;
    } else {
        searchField = [searchBar valueForKey:@"_searchField"];
    }
    searchField.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:searchBar];
    self.searchBar = searchBar;
    
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.tableView registerClass:[SISSPriceMapSearchViewCell class] forCellReuseIdentifier:priceMapSearchViewCellReuseID];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:lineView];
    self.lineView = lineView;
}

#pragma mark - 约束
- (void)initConstraint {
    
    WeakSelf
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(kStatusBarHeight);
        make.left.right.equalTo(weakSelf.view);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.searchBar.mas_bottom).offset(0.5 * globelMargin);
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(1);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.lineView.mas_bottom);
        make.left.right.bottom.equalTo(weakSelf.view);
    }];
}

#pragma mark - 懒加载
- (NSArray<SISSMapSearchPnameModel *> *)pnameArr {
    if (!_pnameArr) {
        _pnameArr = [NSArray array];
    }
    return _pnameArr;
}

@end
